package com.kingzoon.seckill.base.interceptor;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.i18n.LocaleContextHolder;

/**
 * 国际化信息设置
 * @author raymond
 *
 */
public class LocaleInterceptor extends BaseInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HttpSession session = request.getSession();
		Locale locale = (Locale) session.getAttribute("LOCALE");
		if(locale == null){
			String language = request.getParameter("locale");
			if("".equals(language) && language != null){
				locale = new Locale(language);
				session.setAttribute("LOCALE", locale);
			}else{
				locale = request.getLocale();
			}
		}
		session.setAttribute("HOST", "127.0.0.1");
		LocaleContextHolder.setLocale(locale);
		return super.preHandle(request, response, handler);
	}

}
